import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class CustomAppBar extends StatefulWidget implements PreferredSizeWidget{

  const CustomAppBar({
    Key? key,

    this.backgroundColor: Colors.white,
    this.centerTitle: "",
    this.actionName: "",
    this.actionNameColor,
    this.backImg: "assets/images/ic_back_black.png",
    this.onPressed,
    this.backName:"",
    this.isBack: true, this.elevation=0.0
  }): super(key: key);
  final double elevation;
  final Color backgroundColor;
  final String centerTitle;
  final String backImg;
  final String actionName;
  final Color? actionNameColor;
  final VoidCallback? onPressed;
  final bool isBack;
  final String backName;

  @override
  _CustomAppBarState createState() => _CustomAppBarState();

  @override
  Size get preferredSize => Size.fromHeight(60.0);
}

class _CustomAppBarState extends State<CustomAppBar> {

  SystemUiOverlayStyle _overlayStyle = SystemUiOverlayStyle.light;

  @override
  void initState() {
    super.initState();
    setState(() {
      _overlayStyle = ThemeData.estimateBrightnessForColor(widget.backgroundColor) == Brightness.dark
          ? SystemUiOverlayStyle.light
          : SystemUiOverlayStyle.dark;
    });
  }

  Color getColor(){
    return _overlayStyle == SystemUiOverlayStyle.light ? Colors.white : Colors.black87;
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: _overlayStyle,
      child: Material(
       elevation: widget.elevation,
        color: widget.backgroundColor,
        child: SafeArea(
          child: Stack(
            alignment: Alignment.centerLeft,
            children: <Widget>[
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                    alignment: widget.centerTitle.isEmpty ? Alignment.centerLeft : Alignment.center,
                    width: double.infinity,
                    child: Text(
                      widget.centerTitle ,
                      // maxLines: 1,
                      style: TextStyle(fontSize: 18,fontWeight:FontWeight.w400),
                    ),
                    padding: const EdgeInsets.symmetric(horizontal: 20.0),
                  )
                ],
              ),
              widget.isBack?GestureDetector(
                child: Padding(
                  padding: const EdgeInsets.only(left: 20),
                  child: Row(
                    children: <Widget>[
                      Icon(Icons.arrow_back_ios),
                      Text(widget.backName,style: TextStyle(fontSize: 18),)
                    ],
                  ),
                ),onTap: (){
                Navigator.pop(context);
              },
              ):Container(),
              Positioned(
                right: 6,
                child: Theme(
                  data: ThemeData(
                      buttonTheme: ButtonThemeData(
                        padding: const EdgeInsets.symmetric(horizontal: 16.0),
                        minWidth: 60.0,
                      )
                  ),
                  child: widget.actionName.isEmpty ? Container() :
                  FlatButton(
                    child: Text(widget.actionName),
                    textColor: widget.actionNameColor!=null?widget.actionNameColor:getColor(),
                    highlightColor: Colors.transparent,
                    onPressed: widget.onPressed,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}